package sing.core.ext.util

import android.text.TextUtils
//import com.google.gson.Gson
import java.util.regex.Pattern

/**
 * 是否为手机号  0开头 12开头的不支持
 */
fun String.isPhone(): Boolean {
    val mobile = this.replace(" ", "")
//    var pattern = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$"
//    return Pattern.matches(pattern, mobile);
    return mobile.startsWith("1") && mobile.length == 11
}

/**
 * 是否为座机号
 */
fun String?.isTel(): Boolean {
    return this?.let {
        val matcher1 = Pattern.matches("^0(10|2[0-5|789]|[3-9]\\d{2})\\d{7,8}\$", this)
        val matcher2 = Pattern.matches("^0(10|2[0-5|789]|[3-9]\\d{2})-\\d{7,8}$", this)
        val matcher3 = Pattern.matches("^400\\d{7,8}$", this)
        val matcher4 = Pattern.matches("^400-\\d{7,8}$", this)
        val matcher5 = Pattern.matches("^800\\d{7,8}$", this)
        val matcher6 = Pattern.matches("^800-\\d{7,8}$", this)
        return matcher1 || matcher2 || matcher3 || matcher4 || matcher5 || matcher6
    }?:let {
        false
    }
}

/**
 * 是否为邮箱号
 */
fun String.isEmail(): Boolean {
    return Pattern.matches("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}", this)
}

/**
 * 是否为银行卡，简单验证
 */
fun String.isBank(): Boolean {
    return Pattern.matches("^([0-9]{16}|[0-9]{19})\$", this)
}

/**
 * 将对象转为JSON字符串
 */
//fun Any?.toJson():String{
//    return Gson().toJson(this)
//}


fun String?.isBase64Img(): Boolean {
    return this != null && !TextUtils.isEmpty(this) && (this.startsWith("data:image/png;base64,") || this.startsWith("data:image/*;base64,") || this.startsWith("data:image/jpg;base64,"))
}


// 特殊情况，给base64图片添加前缀
fun String?.addBase64Head(): String {
    return "data:image/png;base64,$this"
}

fun String?.get10Length(): String {
    if (this == null){
        return ""
    }else if (this.length > 10){
        return "${this.substring(0,10)}..."
    }else{
        return this
    }
}

fun String?.get10Length1(): String {
    if (this == null){
        return ""
    }else if (this.length > 10){
        return "${this.substring(0,10)}"
    }else{
        return this
    }
}

fun String?.get20Length(): String {
    if (this == null){
        return ""
    }else if (this.length > 20){
        return "${this.substring(0,20)}..."
    }else{
        return this
    }
}


fun String?.getNLength(n:Int): String {
    if (this == null){
        return ""
    }else if (this.length > n){
        return "${this.substring(0,n)}..."
    }else{
        return this
    }
}

fun String?.getMiddleNLength(n:Int): String {
    if (this == null){
        return ""
    }else if (this.length > n){
        return "${this.substring(0, n / 2)}...${this.substring(this.length - n / 2,this.length)}"
    } else {
        return this
    }
}

/**
 * 获取文件名              xxxxxxxxxxx.xxxxxx.x.xxxxxxxxx.mp3
 */
fun String.getFileName(): String {
    if (this.contains("/")){
        val s = this.split("/")
        if (s.size >= 2){
            return s[s.size - 1]
        }
        return this
    }
    return this
}

/**
 * 超过万返回 x万 无小数
 */
fun String?.get0w(): String {
    if (this == null) {
        return ""
    }
    try {
        if (this.toDouble() >= 10000) {
            return "${String.format("%.0f",this.toDouble()/10000)}万"
        }
        return "$this"
    } catch (e: Exception) {
        return this
    }
}